//
//  IAAModifyPwdVC.m
//  iPadAuditAuthorise
//
//  Created by XiangDongMBP on 16/9/21.
//  Copyright © 2016年 XiangDong. All rights reserved.
//

#import "IAAModifyPwdVC.h"
#import "MD5Algorithm.h"

@interface IAAModifyPwdVC ()

@property (weak, nonatomic) IBOutlet UITextField *pwdOldTextField;
@property (weak, nonatomic) IBOutlet UITextField *pwdNewTextField;
@property (weak, nonatomic) IBOutlet UIButton *sureBtn;

@end

@implementation IAAModifyPwdVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //设置导航栏
    [self initNavigationBar];
    //設置子Views
    [self initSubViews];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)hidesBottomBarWhenPushed {
    return YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)initNavigationBar {
    self.navigationItem.title = @"修改密码";
}

- (void)initSubViews {
    [self.pwdOldTextField addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    [self.pwdNewTextField addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    
    self.sureBtn.layer.cornerRadius = 5;
    self.sureBtn.layer.masksToBounds = YES;
    [self.sureBtn addTarget:self action:@selector(sureBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - 按键处理
// 限制输入字符串的长度
- (void)textFieldEditChanged:(UITextField *)field {
    if (field.markedTextRange == nil &&  field.text.length > IAA_PASSWORD_LENGTH) {
        field.text = [field.text substringToIndex:IAA_PASSWORD_LENGTH];
    }
}

- (void)sureBtnTapped:(UIButton *)btn {
    // 校验输入参数
    if (!self.pwdOldTextField.text.length) {
        [MBProgressHUD showPromptWithAutoHideInView:self.view message:@"请输入原密码"];
        return;
    }
    if (!self.pwdNewTextField.text.length) {
        [MBProgressHUD showPromptWithAutoHideInView:self.view message:@"请输入新密码"];
        return;
    }
    // 顯示等待圈圈
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSString *urlStr = [IAAUrlProtocolMerge appModifyPwd];
    NSDictionary *pars = @{@"userNo": IAA_APP_DELEGATE.userNo,
                           @"orgNo": IAA_APP_DELEGATE.organizeId,
                           @"token" : IAA_APP_DELEGATE.token,
                           @"osName": @"iOS",
                           @"opassWord" : [self.pwdOldTextField.text xd_md5L32String],
                           @"npassWord": [self.pwdNewTextField.text xd_md5L32String]};
    
    [XDAFHttpRequest POST:urlStr parameters:pars progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [hud hide:NO];
        NSLog(@"修改密码返回%@", responseObject);
        if ([responseObject[@"resultCode"] isEqualToString:IAA_RESULT_CODE_SUC]) {
            MBProgressHUD *temp = [MBProgressHUD showPromptWithAutoHideInView:self.view message:@"修改密码成功"];
            temp.completionBlock = ^(){
                [self.navigationController popViewControllerAnimated:YES];
            };
        } else {
            [MBProgressHUD showPromptWithAutoHideInView:self.view message:responseObject[@"resultMsg"]];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"修改密码失败%@", error);
        [hud hide:NO];
        [MBProgressHUD showPromptWithAutoHideInView:self.view message:@"网络请求失败"];
    }];
}

@end
